package com.yami.shop.multishop.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.BlogSort;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.BlogSortService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin/blogSort")
public class BlogSortController {

    @Autowired
    private BlogSortService blogSortService;

    @GetMapping("/page")
    @Operation(summary =  "根据参数分页获取广场分类列表", description = "根据参数分页获取广场分类列表")
    public ServerResponseEntity<IPage<BlogSort>> page(PageParam<BlogSort> page, BlogSort blogSort) {
        LambdaQueryWrapper<BlogSort> tagQueryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(blogSort.getStatus()) && blogSort.getStatus() != 0){
            tagQueryWrapper.eq(BlogSort::getStatus,blogSort.getStatus());
        }else{
            tagQueryWrapper.ne(BlogSort::getStatus,0);
        }
        PageParam<BlogSort> tagList = blogSortService.page(page, tagQueryWrapper);
        return ServerResponseEntity.success(tagList);
    }


    @PostMapping
    @Operation(summary =  "新增广场分类", description = "新增广场分类")
    public ServerResponseEntity<Void> saveTag(@RequestBody BlogSort tag){
        QueryWrapper<BlogSort> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("content", tag.getContent());
        queryWrapper.eq("status", 1);
        BlogSort tempTag = blogSortService.getOne(queryWrapper);
        if (tempTag != null) {
            return ServerResponseEntity.showFailMsg("该分类已存在");
        }
        tag.setStatus(1);
        blogSortService.save(tag);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary =  "新增广场分类", description = "新增广场分类")
    public ServerResponseEntity<Void> updateTag(@RequestBody BlogSort tag){
        QueryWrapper<BlogSort> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("content", tag.getContent());
        queryWrapper.ne("uid",tag.getUid());
        queryWrapper.eq("status", 1);
        BlogSort tempTag = blogSortService.getOne(queryWrapper);
        if (tempTag != null) {
            return ServerResponseEntity.showFailMsg("该分类已存在");
        }
        blogSortService.updateById(tag);
        return ServerResponseEntity.success();
    }
}
